﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IMS_Data;

namespace IMS_WEB
{
    public partial class ViewProdects : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        private void Bind()
        {
            ProductReposity pr = new ProductReposity();

            var dataSource = pr.GetProducts(tbCompany.Text,tbProductName.Text);

            GridView1.DataSourceID = null;
            GridView1.DataSource = dataSource;
            GridView1.DataBind();
        }

        protected void btnAddProduct_Click(object sender, EventArgs e)
        {
            Response.Redirect(@"~\NewProdect.aspx");
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            
            

            product c = CreateProduct(e.RowIndex);

            OrderReposity or = new OrderReposity();
            if (or.IsProductInOrder(c.index_id))
            {
                e.Cancel = true;
                Response.Write("<script>alert('" + "该商品已存在于订单中，不能删除" + "')</script>");
                return;
            }

            ProductReposity cr = new ProductReposity();
            cr.DeleteProduct(c);
            Bind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;

            Bind();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                for (int i = 0; i < e.Row.Cells.Count - 2; i++)
                {
                    ////方法一： 
                    //e.Row.Cells[i].Text = " " + e.Row.Cells[i].Text + " ";
                    e.Row.Cells[i].Wrap = false;
                    //方法二： 
                    //e.Row.Cells[i].Text = "<nobr> " + e.Row.Cells[i].Text + " </nobr>"; 
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.RowState == DataControlRowState.Normal
                    || e.Row.RowState == DataControlRowState.Alternate)
                {
                    ((LinkButton)e.Row.Cells[GridView1.Columns.Count - 1].Controls[0]).Attributes.Add("onclick",
                        "javascript:return confirm('你确认要删除该产品吗？')");
                }
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bind();
        }

        private bool CheckDataValid(int RowIndex, ref string errInfo)
        {
            bool result = true;

            string product_name = ((TextBox)GridView1.Rows[RowIndex].Cells[1].Controls[0]).Text.Trim();
            if (string.IsNullOrEmpty(product_name))
            {
                errInfo += "产品名称不能为空;";
                result = false;
            }
            string product_type = ((TextBox)GridView1.Rows[RowIndex].Cells[2].Controls[0]).Text.Trim();

            if (string.IsNullOrEmpty(product_type))
            {
                errInfo += "产品规格不能为空;";
                result = false;
            }
            string product_price = ((TextBox)GridView1.Rows[RowIndex].Cells[4].Controls[0]).Text.Trim();
            float value = 0;
            if (!float.TryParse(product_price, out value))
            {
                errInfo += "产品价格格式不正确（不能为空且应为数字）";
                result = false;
            }

            return result;
        }
        private product UpdateProduct(int RowIndex)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[RowIndex].Values[0].ToString());
            int company_id = Convert.ToInt32(GridView1.DataKeys[RowIndex].Values[1].ToString());
            string product_code = ((TextBox)GridView1.Rows[RowIndex].Cells[1].Controls[0]).Text.Trim();
            string product_name = ((TextBox)GridView1.Rows[RowIndex].Cells[2].Controls[0]).Text.Trim();
            string product_sn = ((TextBox)GridView1.Rows[RowIndex].Cells[3].Controls[0]).Text.Trim();
            string product_mainClass = ((TextBox)GridView1.Rows[RowIndex].Cells[4].Controls[0]).Text.Trim();
            string product_type = ((TextBox)GridView1.Rows[RowIndex].Cells[5].Controls[0]).Text.Trim();
            
            Decimal product_price = Convert.ToDecimal(((TextBox)GridView1.Rows[RowIndex].Cells[6].Controls[0]).Text.Trim());

            string remarks = ((TextBox)GridView1.Rows[RowIndex].Cells[9].Controls[0]).Text.Trim();
            string product_unit = ((TextBox)GridView1.Rows[RowIndex].Cells[8].Controls[0]).Text.Trim();
            Decimal product_salePrice = Convert.ToDecimal(((TextBox)GridView1.Rows[RowIndex].Cells[7].Controls[0]).Text.Trim());

            product c = IMS_Data.product.Createproduct(id, product_name, company_id, product_code);
            c.product_type = product_type;
            c.product_price = product_price;
            c.remarks = remarks;
            //c.product_salePrice = product_salePrice;
            c.product_unit = product_unit;
            c.product_sn = product_sn;
            c.product_mainClass = product_mainClass;
            return c;
        }
        private product CreateProduct(int RowIndex)
        {
            int id = Convert.ToInt32(GridView1.DataKeys[RowIndex].Value);

            product c = new product { index_id = id};

            return c;
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string errInfo = "";
            if (!CheckDataValid(e.RowIndex, ref errInfo))
            {
                e.Cancel = true;
                Response.Write("<script>alert('" + errInfo + "')</script>");
                return;
            }
            
            ProductReposity cr = new ProductReposity();
            cr.UpdateProduct(UpdateProduct(e.RowIndex));
            GridView1.EditIndex = -1;
            Bind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            Bind();
        }

        
    }
}